# -*- coding: utf-8 -*-

'''eof
name:经营到期日期距今时长
code:EG11_OperaToYear
tableName:
columnName:
groups:企业通用模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import json
from datetime import datetime

def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]
# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d')
                except:
                    date = None
    return date
def diff_date(str1):
    try:
        date2 = datetime.now()
        date1 = date_format(str1[0:10])
        diff_year = date1.year - date2.year
        diff_month = date1.month - date2.month
        diff_day = date1.day - date2.day
        age = diff_year*12.0 + diff_month+ diff_day/30.0
        time='%.4f' % age
        if time<float(12):
            return 0
        elif time>=float(12) and time<float(24):
            return 12
        elif time >= float(24) and time < float(36):
            return 24
        elif time>float(36) :
            return 36

    except:
        return u'缺失值'

def EG11_OperaToYear():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        basic_list = FormatData("basicList")
        if basic_list == [{}]:
            return u'缺失值'
        else:
            if len(basic_list) >= 1:
                for i in basic_list:
                    es_date = i.get("openTo")
                    bg_date = i.get("openFrom")
                    # openFrom 和 openTo日期同时为异常才返回缺失
                    if es_date in null_type_list and bg_date in null_type_list:
                        raise ValueError("openTo and openFrom are all empty!")
                    # openTo，空 长期 9999-99-99均为正常且表示为长期3年
                    if es_date == "" or es_date.strip() == u"长期" or es_date.strip() == "9999-99-99":
                        return 36
                    diff_years = diff_date(es_date)
                    if es_date not in null_type_list and diff_years not in null_type_list:
                        return diff_years
                    else:
                        return u'缺失值'
                return u'缺失值'
            else:
                return u'缺失值'
    except:
        return u'缺失值'


if __name__ == "__main__":
    # ratingRequest = {"card_id":"500101199a01122461","bb":""}
    file_obj = open(r'E:\\changjiang_bd\\EG_DSJ.json', 'r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = EG11_OperaToYear()
    print aa

result = EG11_OperaToYear()